package com.example.confirm;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.Date;
import java.util.concurrent.TimeoutException;

//多条异步确认
public class MultipleAsyncConfirm {

    private static final String MULTIPLE_ASYNC_CONFIRM_EXCHANGE = "multipleAsyncConfirmExchange";
    private static final String MULTIPLE_ASYNC_CONFIRM_QUEUE = "multipleAsyncConfirmQueue";

    public static void main(String[] args) throws IOException, TimeoutException, InterruptedException {
        ConnectionFactory factory = new ConnectionFactory();
        if (true) {
            factory.setHost("localhost");
            factory.setPort(5672);
            factory.setUsername("guest");
            factory.setPassword("guest");
            factory.setVirtualHost("/");
        }
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();
        //交换机声明
        channel.exchangeDeclare(MULTIPLE_ASYNC_CONFIRM_EXCHANGE, BuiltinExchangeType.DIRECT, true);
        channel.queueDeclare(MULTIPLE_ASYNC_CONFIRM_QUEUE, true, false, false, null);
        channel.queueBind(MULTIPLE_ASYNC_CONFIRM_QUEUE, MULTIPLE_ASYNC_CONFIRM_EXCHANGE, MULTIPLE_ASYNC_CONFIRM_QUEUE);

        /**
         * 开启 confirm 确认
         */
        channel.confirmSelect();

        long begin = System.currentTimeMillis();//开始时间


        /**
         * 开启 confirm 回调函数
         */
        ConfirmCallback ackCallBack = new ConfirmCallback() {
            /**
             * 成功回调
             * @param deliveryTag
             * @param multiple      true表示已确认多条,false表示已确认单条
             * @throws IOException
             */
            @Override
            public void handle(long deliveryTag, boolean multiple) throws IOException {
                if (multiple) {
                    System.out.println("已确认多条: " + deliveryTag);
                } else {
                    System.out.println("已确认单条: " + deliveryTag);
                }
            }
        };
        ConfirmCallback nackCallBack = new ConfirmCallback() {
            /**
             * 失败回调
             * @param deliveryTag
             * @param multiple      true表示未确认多条,false表示未确认单条
             * @throws IOException
             */
            @Override
            public void handle(long deliveryTag, boolean multiple) throws IOException {
                if (multiple) {
                    System.out.println("未确认多条: " + deliveryTag);
                } else {
                    System.out.println("未确认多条: " + deliveryTag);
                }
            }
        };
        channel.addConfirmListener(ackCallBack, nackCallBack);

        for (int i = 1; i <= 1000; i++) {
            String str = i + "\t" + new Date().getTime();
            channel.basicPublish(MULTIPLE_ASYNC_CONFIRM_EXCHANGE, MULTIPLE_ASYNC_CONFIRM_QUEUE, null, str.getBytes());
        }

        long end = System.currentTimeMillis();//结束时间
        System.out.println("总耗时: " + (end - begin) + " 毫秒==========================");
    }


}
